<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<c:set var="serviceurl" value="${ctx}/bills/bills" />
<html>
<head>
</head>
<body>
	<% String showType = "true".equals(request.getParameter("isMulti"))?"":"hide" ;%>
	<div class="container-fluid rap-select-container" id="rap-container-list">
		<div class = "rap-select-toolbar">
			<button id="ok" class="<%=showType %>"><i class="glyphicon glyphicon-ok"></i> 选择</button>
			<button id="selectNull" class="<%="true".equals(request.getParameter("isMulti"))?"hide":"" %>"><i class=""></i> 清空</button>
		</div>
		<div class="row rap-searchbar hide">
			<form id="rap-form-query" class="form-inline">
				<!-- 默认查询条件 -->
				<input type="hidden" name="search_EQ_status" value="1">
			</form>
		</div>
		<div class="row-fluid">
			<table id="rap-table-list" class="table table-striped table-bordered table-hover">
				<thead>
					<tr>
						<th class="<%=showType %>"><input type="checkbox" /> <span class="lbl"></span></th>
						<th>年月</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
				</tbody>
			</table>
		</div>
	</div>
	
	<script type="text/javascript">
		//是否服务器分页
		var _isServerPage = true;
		//变量定义
		var _tableList, _queryData, _detailRecord, _detailRecordIdx, _validator;
		var _isMulti="${param.isMulti}";//是否多选
		var _tableCols = [{
						data : 'uuid',
						orderable : false,
						className : "center <%=showType %>",
						width : "30",
						render : RapDataTableUtils.renderCheckCol
					},
					{
						data : 'yearMonth',
						width : "120",
						//render : function(data, type, full){},
						orderable : true
					},
					{
						data : "uuid",
						className : "center",
						orderable : false,
						render : function(data, type, full){
							return "<div class='rap-btn-actiongroup'>" 
							+ "<button id='selectBtn' class='btn btn-xs btn-info' data-rel='tooltip' title='选择'>选择</button>"
							+ "</div>";
						},
						width : "80"
					}];
		function appendUrlParams(){
			var _params = "${param}";
			if(_params==null||_params.length==0){
				return false;
			}
			if(_queryData==null){
				_queryData=new Array();
			}
			_params = _params.replace("{","");
			_params = _params.replace("}","");
			var paramObj = _params.split(",");
			
			for(var i=0;i<paramObj.length;i++){
				var nameAndValue = paramObj[i].split("=");
				var name = $.trim(nameAndValue[0]);
				var value = $.trim(nameAndValue[1]);
				if($("#rap-form-query [name='"+name+"']").length>0){
					$("#rap-form-query [name='"+name+"']").val(value);
				}else{
					$("#rap-form-query").append("<input type='hidden' name='"+name+"' value='"+value+"'>");
				}
			} 
		}			
		//加载表格数据
		function loadList() {
			$.ajax({
				url : '${serviceurl}/query',
				data : _queryData,
				dataType : 'json',
				success : function(data) {
					_tableList.clear();
					_tableList.rows.add(data.records);
					_tableList.draw();
				}
			});
		}
		function callBackParent(data){
			//调用传递过来的callback方法
			window.parent.${callback}(data);
			window.parent.$.colorbox.close();
		}
		function callBackParent(data){
			var returnData = data;
			if(_isMulti=="true"){
				returnData = new Array();
				returnData.push(data);
			}
			//调用传递过来的callback方法
			window.parent.${callback}(returnData);
			window.parent.$.colorbox.close();
		}
		function callbackParentMulti(pks){
			var returnData = _tableList.rows("#rap-table-list tr:has(input[name='chkrow']:checked)").data();
			if(returnData==null||returnData.length==0){
				alert("请选择记录！");
				return false;
			}
			if(_isMulti!="true"&&returnData.length>1){
				alert("只能选择一条记录！");
				return false;
			}
			if(_isMulti=="true"){
				window.parent.${callback}(returnData);//多选则返回数组
			}else{
				window.parent.${callback}(returnData[0]);//不是多选则直接返回单行数据
			}
			window.parent.$.colorbox.close();
		}
		function setActions(nRow, aData, iDataIndex){
			$(nRow).dblclick(function() {
				callBackParent(aData);
			});
			$("#selectBtn",nRow).click(function() {
				callBackParent(aData);
			});
		}
		function serverPage(){
			_tableList = $('#rap-table-list').DataTable({
				"columns" : _tableCols,
				"createdRow" : setActions,
				"order": [] ,
				"processing" : true,
				"serverSide": true,
				"searching": false,
				"pagingType": "full_numbers",
				"paging": true,
			     "ajax" : {
					"url" : '${serviceurl}/query',
					"type": 'POST',
					"data" : function(d) {
						 d.orderby = getOrderbyParam(d);
						if (_queryData == null)
							return;
						$.each(_queryData, function(index) {
							if (this['value'] == null || this['value'] == "")
								return;
							d[this['name']] = this['value'];
						});
					},
			  	    "dataSrc" : function(json) {
			  	    	return json.records==null?[]:json.records;
		            }
				} 
			});
		}
		function getOrderbyParam(d){
			var orderby = d.order[0];
			if(orderby!=null&&null!=_tableCols){
				var dir=orderby.dir;
				var orderName = _tableCols[orderby.column].data;
				return orderName+"@"+dir;
			}
			return "";
		}
		$(document).ready(function() {
			appendUrlParams();
			_queryData = $("#rap-form-query").serializeArray();
			if(_isServerPage){
				//服务器分页
				serverPage();	
			}else{
				_tableList = $('#rap-table-list').DataTable({
					"columns" : _tableCols,
					"createdRow" : setActions,
					"order": [] 
				
				});
				loadList();
			}
			RapDataTableUtils.setCheckCol(_tableList);
			$("#ok").bind("click",function(){
				callbackParentMulti();
			});
			$("#selectNull").bind("click",function(){
				var data = {id:"",uuid:"",name:""};
				callBackParent(data);
			});
		});
	</script>
</body>
</html>
